package com.hhit.zhgd.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.hhit.zhgd.domain.dto.BackStageEquipmentMaterialTestingDTO
import com.hhit.zhgd.domain.entity.ZhgdEquipmentMaterialTesting
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdEquipmentMaterialTestingService
import jakarta.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

@RestController
class ZhgdEquipmentMaterialTestingController {

    @Autowired
    private val zhgdEquipmentMaterialTestingService: ZhgdEquipmentMaterialTestingService? = null


    @PostMapping("backStage/importEquipmentMaterialTesting")
    @Throws(Exception::class)
    fun importEquipmentMaterialTesting(@RequestParam("file") multipartFile: MultipartFile?): BaseResp<Any?>? {
        return zhgdEquipmentMaterialTestingService?.importEquipmentMaterialTesting(multipartFile)
    }

    @GetMapping("getEquipmentMaterialTestings")
    fun getEquipmentMaterialTestings(
        @RequestParam(name = "testItem", defaultValue = "") testItem: String?,
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int
    ): BaseResp<Any?>? {
        val pageInfo = Page<ZhgdEquipmentMaterialTesting>(page.toLong(), size.toLong())
        val queryWrapper = QueryWrapper<ZhgdEquipmentMaterialTesting>().like("test_item", testItem)
        return BaseResp.success(zhgdEquipmentMaterialTestingService?.page(pageInfo, queryWrapper))
    }

    /**
     * @描述 确认 交图
     * @参数 [id]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/7/24
     * @修改人和其它信息
     */
    @GetMapping("confirmEmtHandInDrawings")
    fun confirmEmtHandInDrawings(@RequestParam("id") id: Int?): BaseResp<Any?>? {
        return zhgdEquipmentMaterialTestingService?.confirmEmtHandInDrawings(id)
    }

    /**
     * @description 后台管理-进度管理-添加设备材料检测计划
     * @author qianchengcheng
     * @date 2024/6/28 16:29
     */
    @PostMapping("backStage/addEquipmentMaterialTesting")
    fun addEquipmentMaterialTesting(
        @RequestBody backStageEquipmentMaterialTestingDTO: @Valid BackStageEquipmentMaterialTestingDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError!!.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdEquipmentMaterialTestingService?.addEquipmentMaterialTesting(backStageEquipmentMaterialTestingDTO)
    }

    /**
     * @description 后台管理-进度管理-修改设备材料检测计划
     * @author qianchengcheng
     * @date 2024/6/28 17:42
     */
    @PostMapping("backStage/editEquipmentMaterialTesting")
    fun editEquipmentMaterialTesting(
        @RequestBody backStageEquipmentMaterialTestingDTO: @Valid BackStageEquipmentMaterialTestingDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError?.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdEquipmentMaterialTestingService?.editEquipmentMaterialTesting(backStageEquipmentMaterialTestingDTO)
    }

    /**
     * @description 后台管理-进度管理-删除设备材料检测计划
     * @author qianchengcheng
     * @date 2024/6/28 17:46
     */
    @GetMapping("backStage/delEquipmentMaterialTesting")
    fun delEquipmentMaterialTesting(@RequestParam("ids") ids: List<Int?>?): BaseResp<Any?>? {
        return zhgdEquipmentMaterialTestingService?.delEquipmentMaterialTesting(ids)
    }

    @GetMapping("backStage/getEquipmentMaterialTestingPageInfo")
    fun getEquipmentMaterialTestingPageInfo(
        @RequestParam(name = "keyWord") keyWord: String?,
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int
    ): BaseResp<Any?>? {
        return BaseResp.success(zhgdEquipmentMaterialTestingService?.getEquipmentMaterialTestingPageInfo(keyWord, page, size))
    }

    /**
     * @description 后台管理-进度管理-导出设备材料检测计划
     * @author qianchengcheng
     * @date 2024/7/4 10:51
     */
    @GetMapping("backStage/exportEquipmentMaterialTesting")
    fun exportEquipmentMaterialTesting(response: HttpServletResponse?) {
        try {
            zhgdEquipmentMaterialTestingService?.exportEquipmentMaterialTesting(response)
        } catch (e: Exception) {
//            log.error("exportEquipmentMaterialTesting==>导出设备材料检测计划异常!", e)
            e.printStackTrace()
        }
    }
}